package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
)

func main() {
	r, err := http.Get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx05054be971d87487&secret=4c7cd224df5c0102c19301903521dcc6")
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	var resp = &Resp{}
	json.Unmarshal(b, resp)

	if resp.Errcode != 0 {
		fmt.Println(resp.Errmsg)
		return
	}

	body := `{
     "button":[
     {	
          "type":"view",
          "name":"我要下单",
          "url":"http://117bm.cn/wxindex?ver=1"
      },
      {
           "name":"菜单",
           "sub_button":[
           {	
               "type":"click",
               "name":"推荐商家",
               "key":"V1001_TJSJ"
            }]
       }]
 }`
	body_reader := strings.NewReader(body)
	req, err := http.NewRequest("POST", "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+resp.Access_token, body_reader)

	client := http.DefaultClient

	rsp, err := client.Do(req)
	if err != nil {
		fmt.Println(err.Error())
	}
	b, err = ioutil.ReadAll(rsp.Body)
	if err != nil {
		fmt.Println(err.Error())
	}

	fmt.Println(string(b))
}

type Resp struct {
	Errcode      int    `json:"errcode"`
	Errmsg       string `json:"errmsg"`
	Access_token string `json:"access_token"`
	Expires_in   int    `json:"expires_in"`
}
